﻿using DFGK.WMS.Common.Tools;
using DFGK.WMS.Common.Web;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using DFGK_WMS.Repository;
using DFGK_WMS.WebAPI.Controllers.Result;
using DFGK_WMS.WebAPI.Tools;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.POIFS.Crypt.Dsig;

namespace DFGK_WMS.WebAPI.Controllers.设备
{
    /// <summary>
    /// AGV
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AGVController : ControllerBase
    {
        public ITPutawayOrderRepository TPutawayOrderService { get; set; }

        private readonly ILogRepository logRepository;
        public AGVController(ITPutawayOrderRepository tPutawayOrderRepository,ILogRepository logRepository)
        {
            TPutawayOrderService = tPutawayOrderRepository;
            this.logRepository = logRepository;
        }
        /// <summary>
        /// agv任务上报
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult unloadAndDepart(AgvDto dto)
        {
            if (dto.method != "end")
            {

                return Ok(BaseResult.ReturnInfo(1, "任务未结束", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
            }
            var order = TPutawayOrderService.Getputawayorder(x => x.Code == dto.taskCode);
            //查询任务
            if (order == null)
            {
                return Ok(BaseResult.ReturnInfo(1, "未找到该任务", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
            }
            if (order.SourceType == 1)
            {
                PutChooseDto put = new PutChooseDto()
                {
                    taskCode = dto.taskCode,
                    Roadway = order.Roadway,
                    SiteCode = order.SiteCode
                };
                var st= HttpHelper.SendPostRequest("入库拣选", put);
                LogHelper.Info("agv:" + st);
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 4;
                logg.ModuleName = "设备接口";
                logg.UserName = "输送线入库拣选";
                logg.RealName = "输送线入库拣选";
                logg.UserId = 0;
                logg.Description = st;
                //获取前端IP地址

                logRepository.AddTLod(logg);
                return Ok(BaseResult.ReturnInfo(0, "入库任务完成", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
            }else if (order.SourceType == 2)
            {
                return Ok(BaseResult.ReturnInfo(0, "出库任务完成", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
            }
            return Ok(BaseResult.ReturnInfo(0, "", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
        }
        /// <summary>
        /// agv出入库任务
        /// </summary>
        /// <param name="agv"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult getTaskByWes(AgvRequst agv)
        {
            var agvdt = HttpHelper.AgvSendPostRequest("script-api/getTaskByWms", agv);
            LogHelper.Info("agv:" + agvdt);
            return Ok(BaseResult.ReturnInfo(0, agvdt, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
        }
    }
}
